//
//  DeliveryAddressView.m
//  xpkc
//
//  Created by TopSage iOS on 14/12/31.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "DeliveryAddressView.h"

#import "UserMessage.h"
#import "MBProgressHUD.h"


@interface DeliveryAddressView ()<UITableViewDataSource,UITableViewDelegate>
{
    NSString * _provnceStr;
    NSString * _cityStr;
    NSString * _areaStr;
    
    BOOL _isEdit;
}
@end


@implementation DeliveryAddressView{
    
    UITextField *addField;
    UITextField *nameField;
    UITextField *phoneField;
    UITextField *zipcodeField;
    UITextField *detailedField;
    UITextField *emailField;
    NSString * areaValue;
    
    
    UIButton * ProvinceBtn;
    UIButton * CityBtn;
    UIButton * ArearBtn;
    
    UITableView * tabelView;
    
    NSMutableArray * dataArr;
    
    UIButton * allBtn;
    
    
    DeliveryAddressModel * _editModel;
    
}


-(void)setDeliveryModel:(DeliveryAddressModel *)deliveryModel{
    
    
    
    NSLog(@"%@,%@",deliveryModel.pro,deliveryModel.zip);
    
    
    
    
    // zipcodeField.text = self.deliveryModel.zip;
    
    zipcodeField.text = deliveryModel.zip; // @"123";
    
    
    phoneField.text = deliveryModel.tel;
    
    
    
    NSLog(@"%@--%@",deliveryModel.email,deliveryModel.des);
    
    detailedField.text = deliveryModel.des;
    
    
    
    nameField.text = deliveryModel.consignee;
    
    emailField.text = deliveryModel.email;
    
    if (deliveryModel != nil) {
        _isEdit = YES;
        _editModel = deliveryModel;
        [ProvinceBtn setTitle:deliveryModel.pro forState:UIControlStateNormal];
        
        
        
        [CityBtn setTitle:deliveryModel.city forState:UIControlStateNormal];
        
        _provnceStr = deliveryModel.pro;
        _cityStr = deliveryModel.city;
        
        
        
        NSArray * array = [deliveryModel.address componentsSeparatedByString:@" "];
        
        if (![[array lastObject] isEqualToString:@""]) {
            
            [ArearBtn setTitle:[array lastObject] forState:UIControlStateNormal];
            
            _areaStr = [array lastObject];
            
        }else{
            
            [ArearBtn setTitle:@"地区\t🔽" forState:UIControlStateNormal];
        }
        
    }
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //  NSLog(@"%@",self.deliveryModel.pro);
        dataArr = [NSMutableArray array];
        
        [self creatView];
        
        [self createTableView];
        
        
        
        
    }
    return self;
}

- (void)creatView{
    
    
    
    //创建
    nameField = [[UITextField alloc]init];//姓名
    phoneField = [[UITextField alloc]init];//手机
    zipcodeField = [[UITextField alloc]init];//电子邮箱
    detailedField = [[UITextField alloc]init];//详细
    //    addField=[[UITextField alloc] init];//地区
    emailField = [[UITextField alloc] init];
    nameField.backgroundColor=[UIColor whiteColor];
    phoneField.backgroundColor=[UIColor whiteColor];
    zipcodeField.backgroundColor=[UIColor whiteColor];
    detailedField.backgroundColor=[UIColor whiteColor];
    emailField.backgroundColor=[UIColor whiteColor];
    
    
    ProvinceBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    CityBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ArearBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    //信息妥善保存，谨防泄露
    
    //    addressPick = [[UIPickerView alloc]init];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    UILabel *phoneLabel = [[UILabel alloc]init];
    UILabel *zipcodeLabel = [[UILabel alloc]init];
    UILabel *addrLabel = [[UILabel alloc]init];
    //    UILabel *addrText = [[UILabel alloc]init];
    UILabel *detailLabel = [[UILabel alloc]init];
    UILabel *emailLabel = [[UILabel alloc] init];
    //位置
    CGRect name_label_frame,name_field_frame;
    CGRect phone_label_frame,phone_field_frame;
    CGRect zipcode_lable_frame,zipcode_field_frame;
    CGRect addr_label_frame,addr_text_frame;
    CGRect detailed_label_frame,detailed_field_frame;
    CGRect address_picker_frame;
    CGRect email_label_frame,email_field_frame;
    
    CGFloat textH = 35;
    
    CGFloat offwidth = 0.04*SCREEN_WIDTH;
    
    
    
    
    name_label_frame = CGRectMake(offwidth, offwidth, 100, textH);
    name_field_frame = CGRectMake(offwidth+100, offwidth, SCREEN_WIDTH - 100 - 2 * offwidth, textH);
    phone_label_frame = CGRectMake(offwidth, offwidth+40, 100, textH);
    phone_field_frame = CGRectMake(offwidth+100, offwidth+40, SCREEN_WIDTH - 100 - 2 * offwidth, textH);
    zipcode_lable_frame = CGRectMake(offwidth, offwidth+80, 100, textH);
    zipcode_field_frame = CGRectMake(offwidth+100, offwidth+80, SCREEN_WIDTH - 100 - 2 * offwidth, textH);
    addr_label_frame = CGRectMake(offwidth, offwidth+120, 100, textH);
    addr_text_frame = CGRectMake(offwidth+100, offwidth+120, SCREEN_WIDTH - 100 - 2 * offwidth, textH);
    detailed_label_frame = CGRectMake(offwidth, offwidth+160, 100, textH);
    detailed_field_frame = CGRectMake(offwidth+100, offwidth+160, SCREEN_WIDTH - 100 - 2 * offwidth, textH);
    
    email_label_frame = CGRectMake(offwidth,offwidth+200, 100, textH);
    email_field_frame = CGRectMake(offwidth + 100, offwidth + 200, SCREEN_WIDTH - 100 - 2 * offwidth, textH);
    
    address_picker_frame = CGRectMake(offwidth, offwidth+240, SCREEN_WIDTH-offwidth*2, 200);
    
    
    
    
    
    nameLabel.frame = name_label_frame;
    nameField.frame = name_field_frame;
    phoneLabel.frame = phone_label_frame;
    phoneField.frame = phone_field_frame;
    zipcodeLabel.frame = zipcode_lable_frame;
    zipcodeField.frame = zipcode_field_frame;
    addrLabel.frame = addr_label_frame;
    //    addField.frame=addr_text_frame;
    //    addrText.frame = addr_text_frame;
    detailLabel.frame = detailed_label_frame;
    detailedField.frame = detailed_field_frame;
    
    emailLabel.frame = email_label_frame;
    emailField.frame = email_field_frame;
    //    addressPick.frame = address_picker_frame;
    //详细设定
    nameLabel.text = @"收货人：";
    phoneLabel.text = @"手机号码：";
    zipcodeLabel.text = @"邮政编码：";
    addrLabel.text = @"所在地区：";
    detailLabel.text = @"详细地址：";
    emailLabel.text = @"电子邮箱：";
    
    CGFloat provinceBtnW = (SCREEN_WIDTH - 100 - 2 * offwidth - 4 ) / 3;
    
    
    nameField.placeholder=@"请输入姓名";
    phoneField.placeholder=@"请输入电话号码";
    zipcodeField.placeholder=@"请输入邮政编码";
    //    addrText.text = @"北京市北京市北京市";
    detailedField.placeholder=@"请输入详细地址";
    // addField.placeholder=@"请选择地址";
    
    emailField.placeholder = @"请输入电子邮箱";
    
    
    ProvinceBtn.frame = CGRectMake(offwidth + 100, offwidth + 130, provinceBtnW, 20);
    ProvinceBtn.tag =100;
    ProvinceBtn.backgroundColor = [UIColor whiteColor];
    [ProvinceBtn setTitle:@"省份\t🔽" forState:UIControlStateNormal];
    
    [ProvinceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    ProvinceBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [ProvinceBtn addTarget:self action:@selector(ProvinceClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:ProvinceBtn];
    
    CityBtn.frame = CGRectMake(offwidth +100 + provinceBtnW + 2, offwidth +130  , provinceBtnW, 20);
    CityBtn.tag =101;
    CityBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [CityBtn setTitle:@"城市\t🔽" forState:UIControlStateNormal];
    CityBtn.backgroundColor = [UIColor whiteColor];
    
    [CityBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [CityBtn addTarget:self action:@selector(ProvinceClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:CityBtn];
    ArearBtn.frame = CGRectMake(offwidth + 100 + (provinceBtnW + 2 )* 2, offwidth +130  , provinceBtnW, 20);
    ArearBtn.tag =102;
    ArearBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    ArearBtn.backgroundColor = [UIColor whiteColor];
    [ArearBtn setTitle:@"地区\t🔽" forState:UIControlStateNormal];
    
    [ArearBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [ArearBtn addTarget:self action:@selector(ProvinceClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:ArearBtn];
    
    
    
    
    
    
    
    
    
    //        nameField.text = self.deliveryModel.consignee;
    //        phoneField.text = self.deliveryModel.tel;
    //        zipcodeField.text = self.deliveryModel.zip;
    //        //    addrText.text = @"北京市北京市北京市";
    //        detailedField.text = self.deliveryModel.des;;
    //        // addField.placeholder=@"请选择地址";
    //
    //        emailField.text = self.deliveryModel.email;
    
    
    
    
    nameField.textColor = [UIColor blueColor];
    phoneField.textColor = [UIColor blueColor];
    zipcodeField.textColor = [UIColor blueColor];
    addField.textColor=[UIColor blueColor];
    detailedField.textColor = [UIColor blueColor];
    emailField.textColor = [UIColor blueColor];
    
    
    
    [self addSubview:nameLabel];
    [self addSubview:nameField];
    [self addSubview:phoneLabel];
    [self addSubview:phoneField];
    [self addSubview:zipcodeLabel];
    [self addSubview:zipcodeField];
    [self addSubview:addrLabel];
    [self addSubview:addField];
    //    [self addSubview:addrText];
    [self addSubview:detailLabel];
    [self addSubview:detailedField];
    
    [self addSubview:emailLabel];
    [self addSubview:emailField];
    
    //    [self addSubview:addressPick];
    
    
    
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4, SCREEN_HEIGHT-50-70, SCREEN_WIDTH/2, 50)];
    button.backgroundColor=[UIColor redColor];
    [button setTitle:@"保存并使用" forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    button.titleLabel.font=[UIFont boldSystemFontOfSize:19];
    [self addSubview:button];
    [button addTarget:self action:@selector(saveDate) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
}

-(void)proClick:(UIButton *)btn{
    
    if (btn.tag == 40) {
        
    }else if(btn.tag == 41){
        
    }else if (btn.tag == 42){
        
    }
    
}



#pragma mark 选择地区的点击事件
-(void)ProvinceClick:(UIButton *)btn{
    
    [dataArr removeAllObjects];
    CGFloat offwidth = 0.04*SCREEN_WIDTH;
    
    CGFloat provinceBtnW = (SCREEN_WIDTH - 100 - 2 * offwidth - 4 ) / 3;
    
    NSString * path = [[NSBundle mainBundle]pathForResource:@"area" ofType:@"plist"];
    NSArray * arr = [[NSArray alloc]initWithContentsOfFile:path];
    
    if (btn.tag ==100) {
        allBtn =ProvinceBtn;
        for (NSDictionary * dic in arr) {
            [dataArr addObject:dic[@"state"]];
            
        }
        tabelView.frame =CGRectMake(offwidth + 100, offwidth + 130 +20,provinceBtnW, SCREEN_HEIGHT - offwidth -130 -200);
        
    }else if (btn.tag ==101){
        allBtn =CityBtn;
        //        NSLog(@"%@",ProvinceBtn.titleLabel.text);
        if ([ProvinceBtn.titleLabel.text isEqualToString:@"选择省份"]) {
            
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"友情提示" message:@"请选择省份" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            tabelView.frame = CGRectZero;
            
            
            
        }else{
            for (NSDictionary * dic in arr) {
                
                if ([ProvinceBtn.titleLabel.text isEqualToString:dic[@"state"]]) {
                    
                    
                    NSArray * arr2 = dic[@"cities"];
                    for (NSDictionary * dic2 in arr2) {
                        tabelView.frame =CGRectMake(offwidth + 100 +provinceBtnW + 2, offwidth + 130 +20,provinceBtnW, SCREEN_HEIGHT - offwidth -130 -200);
                        [dataArr addObject:dic2[@"city"]];
                        
                        
                    }
                    
                }
            }
        }
    }else{
        
        allBtn = ArearBtn;
        
        if ([CityBtn.titleLabel.text isEqualToString:@"选择城市"]) {
            
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"友情提示" message:@"请选择省份" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            
            tabelView.frame = CGRectZero;
            
        }else{
            
            for (NSDictionary * dic in arr) {
                
                if ([ProvinceBtn.titleLabel.text isEqualToString:dic[@"state"]]) {
                    
                    
                    NSArray * arr2 = dic[@"cities"];
                    for (NSDictionary * dic2 in arr2) {
                        
                        if ([CityBtn.titleLabel.text isEqualToString:dic2[@"city"]]) {
                            [dataArr addObjectsFromArray:dic2[@"areas"]];
                            
                            
                            tabelView.frame = CGRectMake(offwidth + 100 + 2 * (provinceBtnW + 2), offwidth + 130 +20,provinceBtnW, SCREEN_HEIGHT - offwidth -130 -200);
                            
                            if (dataArr.count ==0) {
                                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"友情提示" message:@"亲已经没有更多了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                [alert show];
                                
                                tabelView.frame = CGRectZero;
                            }
                            
                            
                        }
                        
                        
                        
                    }
                }
            }
        }
        
    }
    
    
    
    [tabelView reloadData];
    
    tabelView.hidden = NO;
}
//创建一张表用于显示主要的城市地区
-(void)createTableView{
    //    CGFloat offwidth = 0.04*SCREEN_WIDTH;
    
    tabelView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    tabelView.hidden = YES;
    
    tabelView.delegate = self;
    
    tabelView.dataSource = self;
    
    [self addSubview:tabelView];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tabelView deselectRowAtIndexPath:indexPath animated:YES];
    
    tabelView.hidden = YES;
    if (allBtn ==ProvinceBtn) {
        // ProvinceBtn.titleLabel.text = dataArr[indexPath.row];
        
        _provnceStr = dataArr[indexPath.row];
        [ProvinceBtn setTitle:dataArr[indexPath.row] forState:UIControlStateNormal];
        [CityBtn setTitle:@"城市\t🔽" forState:UIControlStateNormal];
        [ArearBtn setTitle:@"地区\t🔽" forState:UIControlStateNormal];
        
        _cityStr = @"";
        _areaStr = @"";
    }else if(allBtn ==CityBtn){
        
        _cityStr = dataArr[indexPath.row];
        [CityBtn setTitle:dataArr[indexPath.row] forState:UIControlStateNormal];
        [ArearBtn setTitle:@"地区\t🔽" forState:UIControlStateNormal];
        _areaStr = @"";
    }else{
        _areaStr = dataArr[indexPath.row];
        [ArearBtn setTitle:dataArr[indexPath.row] forState:UIControlStateNormal];
        
    }
    
    
    
    
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * str = @"NewCell";
    UITableViewCell * cell = [tabelView dequeueReusableCellWithIdentifier:str];
    
    if (cell ==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    cell.textLabel.text = dataArr[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:10];
    return cell;
    
}


- (BOOL)checkValidityTextField
{

    if ([nameField.text isEqualToString:@""]||nameField.text==nil) {
        ShowAlertMsg(@"收货人不能为空");
        return NO;
    }
    if ([phoneField.text isEqualToString:@""]||phoneField.text==nil) {
        ShowAlertMsg(@"手机号码不能为空");
        
        return NO;
    }else if (![self isPhone]){
        ShowAlertMsg(@"手机号码不正确");
        return NO;
    }
    if ([zipcodeField.text isEqualToString:@""]||zipcodeField.text==nil) {
        ShowAlertMsg(@"邮编不能为空");
        
        return NO;
    }
    
    if ([detailedField.text isEqualToString:@""]||detailedField.text==nil) {
        ShowAlertMsg(@"详细地址不能为空");
        
        return NO;
    }
    if ([emailField.text isEqualToString:@""]||emailField.text==nil) {
        ShowAlertMsg(@"电子邮箱不能为空");
        
        return NO;
    }else if (![self isEmile]){
        ShowAlertMsg(@"邮箱格式不正确");
        return NO;
    }
    
  NSLog(@"%@",ProvinceBtn.titleLabel.text);
     return YES;
    
}

//邮箱判断

-(BOOL)isEmile
{
    BOOL isEmile =[self isValidateEmail:emailField.text];
    if (isEmile) {
        return YES;
    }else{
        return NO;
    }
}


-(BOOL)isValidateEmail:(NSString *)email

{
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
    
    return [emailTest evaluateWithObject:email];
    
}



//手机号码判断
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
-(BOOL)isPhone
{
    BOOL isPhone =[self isMobileNumber:phoneField.text];
    if (isPhone) {
        return YES;
    }else{
        return NO;
    }
}


- (void)saveDate{
    
    
  
    
    if ([self checkValidityTextField]) {
        
    
    
    NSDictionary * parameter;
    NSString * urlString;
    
    if (_areaStr == nil) {
        _areaStr = @"";
    }
    
    if (!_isEdit) {
        
        
        NSString *module = @"address";
        
        
        NSString *interface = @"creataddress";
        
        NSString *timeString = [GetTime getTime];
        NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
        urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
        
        //        NSString * address = [NSString stringWithFormat:@"%@%@%@%@",ProvinceBtn.titleLabel.text,CityBtn.titleLabel.text,ArearBtn.titleLabel.text,detailedField.text];
        NSString * address = [NSString stringWithFormat:@"%@ %@ %@",_provnceStr,_cityStr,_areaStr];
        
        parameter = @{@"time":timeString,@"uid":[UserMessage sharedUserMessage].userMid,@"name":nameField.text,@"address":address,@"zip":zipcodeField.text,@"tel":phoneField.text,@"email":emailField.text,@"des":detailedField.text,@"pro":ProvinceBtn.titleLabel.text,@"city":CityBtn.titleLabel.text,@"pagecode":@"1"};
    }else{
        
        
                NSString *module = @"address";
        
        
        NSString *interface = @"modaddress";
        
        NSString *timeString = [GetTime getTime];
        NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
        urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
        
        
        
        
        
        NSString * address = [NSString stringWithFormat:@"%@ %@ %@",_provnceStr,_cityStr,_areaStr];
        
        if (_provnceStr == nil) {
            
            address = self.deliveryModel.address;
        }

        
        parameter = @{@"time":timeString,@"id":_editModel.codeId,@"name":nameField.text,@"address":address,@"zip":zipcodeField.text,@"tel":phoneField.text,@"email":emailField.text,@"des":detailedField.text,@"pro":ProvinceBtn.titleLabel.text,@"city":CityBtn.titleLabel.text,@"pagecode":@"1"};
        
        
    }
    
    
//    NSLog(@"%@--%@",parameter,urlString);
  

    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        logdebug(@"%@",dic);
        
        if ([[dic objectForKey:@"succ"] intValue]) {
            
            [self performSelectorOnMainThread:@selector(showWindow) withObject:nil waitUntilDone:YES];
            
        }
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       
//        NSLog(@"%@",error);
        
        //[self performSelectorOnMainThread:@selector(showWindow) withObject:nil waitUntilDone:YES];
    }];
    
  
        
    }else{
        
    }
}


-(void)showWindow{
    
    
    [self.viewController.navigationController popViewControllerAnimated:YES];
    
    
}



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
    [nameField resignFirstResponder];
    [phoneField resignFirstResponder];
    [zipcodeField resignFirstResponder];
    [detailedField resignFirstResponder];
    [emailField resignFirstResponder];
    
}

@end
